$(document).ready(function(){
    //$('.custom-styles').trigger('click');
    $('.custom-styles-done').bind('click', function(){
        $.modal.close();
    });
    /*STATUS HANDLERI   0(delete) -> 1(open) -> 2(close)*/
    $('.delete-poll').live('click', function(){
        status_modal(this.id,0);
    });
    $('.open-poll').live('click', function(){
        status_modal(this.id,1);
    });
    $('.close-poll').live('click', function(){
        status_modal(this.id,2);
    });

    var del_title = 'Obriši vijest';
    var del_text = 'Da li ste sigurni da želite da obrišete ovu vijest?';

    var open_title = 'Objavite vijest na sajtu';
    var open_text = 'Da li ste sigurni da želite da ponovo objavite ovu vijest na sajtu.'; 

    var close_title = 'Zatvori vijest';
    var close_text = 'Da li ste sigurni da želite da zatvorite vijest na sajtu?';

    function status_modal(id,status){
        switch(status){
            case 0:
                $('#status-title').html(del_title);
                $('#status-text').html(del_text);
                break;
            case 1:
                $('#status-title').html(open_title);
                $('#status-text').html(open_text);
                break;
            case 2:
                $('#status-title').html(close_title);
                $('#status-text').html(close_text);
                break;
        }
        $("#poll-status-modal").modal({
            opacity: 50,
            containerCss:{
                border: 0,
                padding: 0,
                width:'429px',
                height:'218px'
            },
            overlayCss:{
                background: '#000000'
            },
            onOpen: function (dialog) {
                dialog.overlay.fadeIn('slow', function () {
                    dialog.data.hide();
                    dialog.container.fadeIn('slow', function () {
                        dialog.data.slideDown('slow',function(){
                            $('.confirm-poll-status').attr('id', status+'-'+id); 
                            $('.zmodalClose').fadeIn('fast').delay(118000);
                        });
                    });
                });
            },
            closeClass: 'zmodalClose'
        });
        $('.zmodalClose').css({
            'margin-right':'0',
            'margin-top':'0',
            'display':'none'
        });
    }
    function anketa_status(id,status){
        $.ajax({
            type: 'POST',
            url: base_url+'vesti/uredi_vesti/vesti_status/'+id+'/'+status,
            dataType: 'json',
            success: function(data){
                if(data.success=='failed'){
                    alert('Nešto nije u redu.')
                }else{
                    window.location=base_url; 
                }
            },
            error:function(data){alert("Error: " + data);}
        });
    }
    $('.confirm-poll-status').live('click', function(){
        var tmp = this.id;
        var status= tmp.substring(0, 1);
        var id= tmp.substring(2, tmp.length);
        anketa_status(id,status);
    });

})